home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / DOWNLOAD.CGI-S=COUNTER&C=TXT&F=COUNTER.PL < prev    next >
Perl Script  |  1996-06-03  |  7KB  |  231 lines

  1. #!/usr/local/bin/perl
  2. ##############################################################################
  3. # Counter                       Version 1.1.1                                #
  4. # Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
  5. # Created 10/27/95              Last Modified 4/25/96                        #
  6. # Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
  7. ##############################################################################
  8. # COPYRIGHT NOTICE                                                           #
  9. # Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
  10. #                                                                            #
  11. # Counter may be used and modified free of charge by anyone so long as       #
  12. # this copyright notice and the comments above remain intact.  By using this #
  13. # code you agree to indemnify Matthew M. Wright from any liability that      #  
  14. # might arise from it's use.                                                 #  
  15. #                                                                            #
  16. # Selling the code for this program without prior written consent is         #
  17. # expressly forbidden.  In other words, please ask first before you try and  #
  18. # make money off of my program.                                              #
  19. #                                                                            #
  20. # Obtain permission before redistributing this software over the Internet or #
  21. # in any other medium.    In all cases copyright and header must remain intact.#
  22. ##############################################################################
  23. # Define Variables
  24.  
  25.     ### FILE AND DIRECTORY LOCATIONS, REFERERS ###
  26.  
  27. $count_file = "/path/to/count.txt";
  28. $digit_dir = "/path/to/digit_dir";
  29. $access_log = "/path/to/access_log";
  30. $error_log = "/path/to/error_log";
  31.  
  32. $flyprog = "/path/to/fly/fly -q";
  33. $fly_temp = "/path/to/fly_temp.txt";
  34.  
  35. $bad_referer_img = "http://www.host.com/path/to/bad_referer.gif";
  36.  
  37. @referers = ("www.worldwidemart.com","worldwidemart.com","206.31.72.203");
  38.  
  39.     ### IMAGE SETTINGS ###
  40.  
  41. $width = "24";
  42. $height = "28";
  43.  
  44. $tp = "X";
  45. $il = "1";
  46.  
  47. $frame_width = "8";
  48. $frame_color = "0,0,0";
  49.  
  50. $dot = "X";
  51. $logo = "X";
  52.  
  53.     ### OPTIONS ###
  54.  
  55. $uselog = "1";    # 1 = YES; 0 = NO
  56.  
  57. # Done
  58. ##############################################################################
  59.  
  60. # Get the Date For Logging Purposes
  61. if ($uselog == 1) {
  62.    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  63.    if ($sec < 10)  { $sec = "0$sec";   }
  64.    if ($min < 10)  { $min = "0$min";   }
  65.    if ($hour < 10) { $hour = "0$hour"; }
  66.    if ($mday < 10) { $mday = "0$mday"; }
  67.    if ($mon < 10)  { $monc = "0$mon";  }
  68.    $date = "$hour\:$min\:$sec $mon/$mday/$year";
  69. }
  70.  
  71. # Make Sure People Aren't Messing With the Counter From Other Web Pages
  72. &check_referer;
  73.  
  74. # Get the Counter Number And Write New One to File
  75. &get_num;
  76.  
  77. # If they Just want a transparent dot or a logo, give them that.
  78. &check_dot;
  79.  
  80. # Determine Length of Counter Number
  81. $num = $length = length($count);
  82.  
  83. # Set Individual Counter Numbers Into Associative Array
  84. while ($num > 0) {
  85.    $CHAR{$num} = chop($count);
  86.    $num--;
  87. }
  88.  
  89. # Determine the Height and Width of the Image
  90. $img_width = (($width * $length) + ($frame_width * 2));
  91. $img_height = (($frame_width * 2) + $height);
  92.  
  93. # Open the In-File for Commands
  94. open(FLY,">$fly_temp") || die "Can't Open In File For FLY Commands: $!\n";
  95.  
  96. # Create New Counter Image
  97. print FLY "new\n";
  98. print FLY "size $img_width,$img_height\n";
  99.  
  100. # If User Wants Frame, Print Commands to the In-File
  101. &make_frame;
  102.  
  103. # Copy Individual Counter Images Commands to In-File
  104. $j = 1;
  105. while ($j <= $length) {
  106.    print FLY "copy $insert_width,$insert_height,-1,-1,-1,-1,$digit_dir/$CHAR{$j}\.gif\n";
  107.    $insert_width = ($insert_width + $width); 
  108.    $j++;
  109. }
  110.  
  111. # If they want a color transparent, make it transparent
  112. if ($tp ne "X" && $tp =~ /.*,.*,.*/) {
  113.    print FLY "transparent $tp\n";
  114. }
  115.  
  116. # If they want the image interlaced, make it interlaced
  117. if ($il == 1) {
  118.    print FLY "interlace\n";
  119. }
  120.  
  121. # Close FLY
  122. close(FLY);
  123.  
  124. $output = `$flyprog -i $fly_temp`;
  125. print "Content-type: image/gif\n\n";
  126. print "$output";
  127.  
  128. # Remove Temp File
  129. unlink($fly_temp);
  130.  
  131. # Log the Counter Access
  132. if ($uselog == 1) {
  133.    &log_access;
  134. }
  135.  
  136. sub check_referer {
  137.    if (@referers && $ENV{'HTTP_REFERER'}) {
  138.       foreach $referer (@referers) {
  139.          if ($ENV{'HTTP_REFERER'} =~ /$referer/) {
  140.             $ref = 1;
  141.             last;
  142.          }
  143.       }
  144.    }
  145.    else {
  146.       $ref = 1;
  147.    }
  148.  
  149.    if ($ref != 1) {
  150.       print "Location: $bad_referer_img\n\n";
  151.  
  152.       if ($uselog == 1) {
  153.          open(LOG,">>$error_log") || die "Can't Open User Error Log: $!\n";
  154.          print LOG "$error: $ENV{'REMOTE_HOST'} [$date] $ENV{'HTTP_REFERER'} - $ENV{'HTTP_USER_AGENT'}\n";
  155.          close(LOG);
  156.       }
  157.  
  158.       exit;
  159.    }
  160. }
  161.  
  162. sub get_num {
  163.    open(COUNT,"$count_file") || die "Can't Open Count Data File: $!\n"; 
  164.    $count = <COUNT>;
  165.    close(COUNT);
  166.    if ($count =~ /\n$/) {
  167.       chop($count);
  168.    }
  169.  
  170.    $count++;
  171.  
  172.    open(COUNT,">$count_file") || die "Can't Open Count Data File For Writing: $!\n";
  173.    print COUNT "$count";
  174.    close(COUNT);
  175. }
  176.  
  177. sub check_dot {
  178.  
  179.    if ($dot == 1) {
  180.       # Open the In-File for Commands
  181.       open(FLY,">$fly_temp") || die "Can't Open In File For FLY Commands: $!\n";
  182.  
  183.       # Create New Counter Image
  184.       print FLY "new\n";
  185.       print FLY "size 1,1\n";
  186.       print FLY "fill x,y,0,0,0\n";
  187.       print FLY "transparent 0,0,0\n";
  188.       close(FLY);
  189.  
  190.       $output = `$flyprog -i $fly_temp`;
  191.       print "Content-type: image/gif\n\n";
  192.       print "$output";
  193.  
  194.       exit;
  195.    }
  196.    elsif ($logo ne "X" && $logo =~ /.*tp:\/\//) {
  197.       print "Location: $logo\n\n";
  198.  
  199.       # Log The Access
  200.       if ($uselog == 1) {
  201.          &log_access;
  202.       }
  203.  
  204.       exit;
  205.    }
  206. }
  207.  
  208. sub make_frame {
  209.    $insert_width = $insert_height = $frame_width;
  210.  
  211.    $insert_frame = 0;
  212.  
  213.    while ($insert_frame < $frame_width) {
  214.       $current_width = ($img_width - $insert_frame);
  215.       $current_height = ($img_height - $insert_frame);
  216.  
  217.       print FLY "line 0,$insert_frame,$img_width,$insert_frame,$frame_color\n";
  218.       print FLY "line $insert_frame,0,$insert_frame,$img_height,$frame_color\n";
  219.       print FLY "line $current_width,0,$current_width,$img_height,$frame_color\n";
  220.       print FLY "line $current_height,0,$current_height,$img_width,$frame_color\n";
  221.  
  222.       $insert_frame++;
  223.    }
  224. }
  225.  
  226. sub log_access {
  227.    open(LOG,">>$access_log") || die "Can't Open User Access Log: $!\n";
  228.    print LOG "[$date] $ENV{'HTTP_REFERER'} - $ENV{'REMOTE_HOST'} -  $ENV{'HTTP_USER_AGENT'}\n";
  229.    close(LOG);
  230. }
  231.